<template>
 <div class="login">
    <el-card class="box-card login-inner">
      <div slot="header" class="clearfix">
        <span>登录</span>
      </div>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="账号" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input
            type="password"
            v-model="ruleForm.pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >登录</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>

</template>

<script>
export default {
  name: "login",
  data() {
    var checkUsername = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("账号不能为空"));
      }
      callback();
    };
    var validatePass = (rule, value, callback) => {
      let reg = /^[\w]{2,12}$/;
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (!reg.test(value)) {
          callback(new Error("密码只能是字母,数字,下划线长度为2-12"));
        }
        callback();
      }
    };

    return {
      ruleForm: {
        username: "admin",
        pass: "123456",
      },
      rules: {
        pass: [
          {
            required: true,
            validator: validatePass,
            trigger: "blur",
          },
        ],
        username: [
          { required: true, validator: checkUsername, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
   async submitForm(formName) {
     let res = await this.$http.post('/visit/admin/login',{admin_name:this.ruleForm.username,pwd:this.ruleForm.pass,})
     console.log(res);
    if(res.code=='00000'){
          sessionStorage.setItem('token',res.token)
          sessionStorage.setItem('user', JSON.stringify(res.admin))
           this.$router.push('/admin')
           this.$message.success("登陆成功");
      }else{
                     this.$message.error(res.msg);
             }
    },

    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style>
.login{
    position: fixed;
    height: 100%;
    width: 100%;
    background-color: #658ea9 !important;
}
.login-inner {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 400px;
}
.clearfix {
  text-align: center;
  font-size: 25px;
}
</style>

